package com.example.autojob.skeleton.framework.task;

import com.example.autojob.logging.model.producer.AutoJobLogHelper;
import com.example.autojob.skeleton.framework.boot.AutoJobApplication;
import com.example.autojob.skeleton.framework.mail.IMailClient;
import com.example.autojob.util.convert.DefaultValueUtil;
import lombok.Getter;

import java.util.Collections;
import java.util.List;

/**
 * 任务运行上下文
 *
 * @author Huang Yongxiang
 * @date 2023-01-11 9:23
 * @email 1158055613@qq.com
 */
@Getter
public class AutoJobTaskContext {
    /**
     * 当前执行的任务ID
     */
    private final long taskId;
    /**
     * 当前任务持有的logHelper
     */
    private final AutoJobLogHelper logHelper;
    /**
     * 当前任务持有的邮件客户端
     */
    private final IMailClient mailClient;
    /**
     * 此次调度的启动时间
     */
    private final long startTime;
    /**
     * 任务类型
     */
    private final AutoJobTask.TaskType taskType;
    /**
     * 总分片数
     */
    private final int shardingTotal;
    /**
     * 当前获得的分片数
     */
    private final List<Integer> currentSharding;

    public AutoJobTaskContext(AutoJobTask task) {
        if (task == null) {
            throw new NullPointerException("无法创建上下文");
        }
        taskId = task.id;
        logHelper = task.logHelper;
        taskType = task.type;
        mailClient = DefaultValueUtil.defaultValue(task.mailClient, AutoJobApplication
                .getInstance()
                .getMailClient());
        startTime = task.trigger.triggeringTime;
        shardingTotal = 0;
        currentSharding = Collections.emptyList();
    }
}
